package com.mhs.hotcall;

import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.mhs.BUS.BenhVienBUS;
import com.mhs.DTO.BenhVienDTO;
import com.mhs.moreadapter.ExpandableListView_Adapter;
import com.mhs.mydialog.MyDialogListner;

import com.mhs.map.GMap;
import com.mhs.mhs_gui.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;

public class TraCuuBenhVien_Search_Detail extends MapActivity {
	ExpandableListView_Adapter adapter;
	public final Context THIS = this;
	MapView mapView;
	MapController mc;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tracuubenhvien_search_detail);
		BenhVienDTO benhvienDTO = new BenhVienDTO();
		int maBenhVienClient = 0;
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			maBenhVienClient = extras.getInt("maBV");
			BenhVienBUS benhvienBUS = new BenhVienBUS();
			benhvienDTO = benhvienBUS.getBenhVienFromMaBVClient(this,
					maBenhVienClient);
			ExportData(benhvienDTO);
		}

		mapView = (MapView) findViewById(R.id.MapViewDetailsHospital);
		mapView.setBuiltInZoomControls(true);
		mapView.setTraffic(false);
		mapView.setSatellite(false);
		mc = mapView.getController();
		mc.setZoom(17);

		Drawable drawable = this.getResources().getDrawable(
				R.drawable.normal_marker);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
				drawable.getIntrinsicHeight());
		if (benhvienDTO == null) {
			// do nothing
			MyDialogListner.ShowMessageAlert(this,
					"Có lỗi xảy ra, vui lòng thử lại!");
			return;
		} else {
			double lat = Double.parseDouble(benhvienDTO.getLATITUDE());
			double lon = Double.parseDouble(benhvienDTO.getLONGITUDE());
			GeoPoint geoPointBenhVien = GMap.getPoint(lat, lon);
			mapView.getController().animateTo(geoPointBenhVien);
			mapView.getOverlays().add(new HospitalOverlayItem(drawable, benhvienDTO));
			mapView.invalidate();
		}

	}

	public void ExportData(BenhVienDTO benhvienDTO) {
		if (benhvienDTO == null) {
			// do nothing
			MyDialogListner.ShowMessageAlert(this,
					"Có lỗi xảy ra, vui lòng thử lại!");
			return;
		} else {
			TextView tv_tenbenhvien = (TextView) findViewById(R.id.tv_tenbenhvien);
			tv_tenbenhvien.setText(benhvienDTO.getTENBV());

			TextView tv_diachibenhvien = (TextView) findViewById(R.id.tv_diachibenhvien);
			tv_diachibenhvien.setText("Địa chỉ:" + benhvienDTO.getDIACHI());

			TextView tv_sodienthoaibenhvien = (TextView) findViewById(R.id.tv_sodienthoaibenhvien);
			tv_sodienthoaibenhvien
					.setText("SĐT: " + benhvienDTO.getDIENTHOAI());

		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	class HospitalOverlayItem extends ItemizedOverlay<OverlayItem> {
		public List<OverlayItem> items = new ArrayList<OverlayItem>();
		private Drawable marker = null;
		
		private long lastTouchTimeDown = -1;
		private long lastTouchTimeUp = -1;
		BenhVienDTO bvDTO = new BenhVienDTO();
		public HospitalOverlayItem(Drawable defaultMarker) {
			super(defaultMarker);
			this.marker = defaultMarker;
			
			// add marker vo:
//			items.add(new OverlayItem(GMap.getPoint(10.844438360875152, 106.64966631207278), 
//					"abc", 
//					"abcdef"));
//			
//			items.add(new OverlayItem(GMap.getPoint(10.845365636246356, 106.64760637554934), 
//					"def", 
//					"defghj"));
			
			populate();
		}

		public HospitalOverlayItem(Drawable drawable, BenhVienDTO benhvienDTO) {
			super(drawable);
			this.marker = drawable;
			bvDTO = benhvienDTO;
			double lat = Double.parseDouble(benhvienDTO.getLATITUDE());
			double lon = Double.parseDouble(benhvienDTO.getLONGITUDE());
			GeoPoint geoPointBenhVien = GMap.getPoint(lat, lon);
			OverlayItem ovItemBenhVien = new OverlayItem(geoPointBenhVien,
					benhvienDTO.getTENBV(), benhvienDTO.getDIACHI());
			// add marker vo:
			items.add(ovItemBenhVien);		
			populate();
		}
		
		@Override
		protected OverlayItem createItem(int i) {
			return (items.get(i));
		}

		@Override
		public int size() {
			return (items.size());
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);

			boundCenterBottom(marker);
		}
		
		protected boolean onTap(int i) {
			//if time Difference between lastTouchTimeUp & lastTouchTimeDown is:
			// > 1500 millisec. it was a LONG TAP
			// < 1500 just a NORMAL tap
			// on LONG TAPs we may want to show a dialog box with additional 
			// data about item i-th such as pictures, links to web-sites, ???, etc. 
			//---------------------------------------------------------------------
			String text = "NORMAL TAP";
			long pressTotalTime = lastTouchTimeUp - lastTouchTimeDown;
			if (pressTotalTime > 1500) {
				text = "LONG TAP";
			}
			
			Toast.makeText(TraCuuBenhVien_Search_Detail.this,
					items.get(i).getTitle() + "\n" +
					items.get(i).getSnippet(), Toast.LENGTH_SHORT).show();

			return (true);
		}
		
		// TODO implement longPress actions (such as dialog box showing
		//      pictures, links, ???, of selected point.
		public boolean onTouchEvent(MotionEvent event, MapView mapView) {
			//remember the initial time the user touches the screen
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				lastTouchTimeDown = event.getDownTime();
				lastTouchTimeDown = System.currentTimeMillis();
			}
			if (event.getAction() == MotionEvent.ACTION_UP) {
				lastTouchTimeUp = System.currentTimeMillis();
			}			
			return super.onTouchEvent(event, mapView);
		}
	}
}